package com.km.sglototo.dict.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.dict.entity.TbDict;
import com.km.sglototo.dict.service.TbDictService;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author xt
 * @since 2022-04-18
 */
@RestController
@RequestMapping("/dict")
public class TbDictController {
    @Resource
    private TbDictService tbDictService;

    @SysLog(MODULE = "dict", REMARK = "字典查询")
    @ApiOperation("字典查询")
    @GetMapping(value = "/list")
    public Object getList(TbDict entity,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<TbDict> queryWrapper = new QueryWrapper<>();
        if (entity != null) {
            queryWrapper.eq("delete_id", 0);
        }
        if (entity.getName() != null) {
            queryWrapper.like("name", entity.getName());
        } else if (entity.getDescription() != null) {
            queryWrapper.like("description", entity.getDescription());
        }
        Page<TbDict> p = new Page<TbDict>(pageNum, pageSize);
        return new CommonResult().success(tbDictService.page(p, queryWrapper));

    }

    @SysLog(MODULE = "sysDict_add", REMARK = "新增字典")
    @ApiOperation("新增字典")
    @PostMapping(value = "/add")
    public Object addSysDict(@RequestBody TbDict entity) {
        boolean b = false;
        if (entity.getDictId() != null) {
            b = tbDictService.updateById(entity);
        } else {
            b = tbDictService.save(entity);
        }
        return new CommonResult().success(b);
    }


    @SysLog(MODULE = "Updata", REMARK = "更新字典信息")
    @ApiOperation("更新字典信息")
    @PostMapping(value = "/update/{id}")
    public Object updateInterview(@RequestBody TbDict entity) {
        boolean b = tbDictService.updateById(entity);
        return new CommonResult().success();
    }

    /*真删*/
//    @SysLog(MODULE = "sysDict_delete", REMARK = "删除字典信息")
//    @ApiOperation("删除字典信息")
//    @GetMapping(value = "/delete/{id}")
//    public Object deleteInterview(@ApiParam("用户id") @PathVariable Long id) {
//        try {
//            if (ValidatorUtils.empty(id)) {
//                return new CommonResult().paramFailed("用户id");
//            }
//            QueryWrapper<TbDict> qw = new QueryWrapper<>();
//            qw.eq("dict_id", id);
//            if (tbDictService.remove(qw)) {
//                return new CommonResult().success();
//            }
//        } catch (Exception e) {
//            // log.error("删除用户：%s", e.getMessage(), e);
//            return new CommonResult().failed();
//        }
//        return new CommonResult().failed();
//    }

    /*逻辑删除*/

    @SysLog(MODULE = "sysDict_delete", REMARK = "删除字典信息")
    @ApiOperation("删除字典信息")
    @GetMapping(value = "/delete/")
    public Object deleteUser(@RequestParam Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("用户id");
            }
            TbDict tbDict = tbDictService.getById(id);
            tbDict.setDeleteId(1);
            /*逻辑删除*/
            if (tbDictService.saveOrUpdate(tbDict)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }






    @SysLog(MODULE = "sysDict_deletes", REMARK = "批量删除字典信息")
    @ApiOperation("批量删除字典信息")
    @GetMapping(value = "deleteBatch")
    public Object deleteInterviewBatch(Integer[] ids) {
        List<Integer> list = new ArrayList<>();
        for (Integer id : ids) {
            list.add(id);
        }
        if (tbDictService.removeByIds(list)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed();
    }






    @SysLog(MODULE = "get_sysDict ", REMARK = "根据id获取字典信息表")
    @ApiOperation("根据id获取字典信息表")
    @GetMapping(value = "/getById/{id}")
    public Object getById(@ApiParam("sysDict_id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("id");
            }
            TbDict obj = null;
            if (id != null) {
                obj = tbDictService.getById(id);
                return new CommonResult().success(obj);
            }

        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }
    
}

